package com.lkhhd.activity.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lkhd.activity.service.ActivityService;
import com.lkhd.common.exception.ActivityException;
import com.lkhd.common.myemum.ActivityEnum;
import com.lkhd.common.myemum.CategoryEnum;
import com.lkhd.common.utils.IDUtil;
import com.lkhd.manage.mapper.ActivityMapper;
import com.lkhd.manage.mapper.ActivitymultipartcategoryMapper;
import com.lkhd.manage.mapper.ActivityuserplanMapper;
import com.lkhd.manage.mapper.AreasMapper;
import com.lkhd.manage.mapper.CategoryMapper;
import com.lkhd.manage.mapper.TicketMapper;
import com.lkhd.manage.mapper.TickettypeMapper;
import com.lkhd.manage.mapper.UserMapper;
import com.lkhd.manage.mapper.UsercommentMapper;
import com.lkhd.manage.mapper.VolunteerMapper;
import com.lkhd.manage.pojo.Activity;
import com.lkhd.manage.pojo.Activitymultipartcategory;
import com.lkhd.manage.pojo.Activityuserplan;
import com.lkhd.manage.pojo.Areas;
import com.lkhd.manage.pojo.Category;
import com.lkhd.manage.pojo.Ticket;
import com.lkhd.manage.pojo.Tickettype;
import com.lkhd.manage.pojo.User;
import com.lkhd.manage.pojo.Usercomment;
import com.lkhd.manage.pojo.Volunteer;
import com.lkhd.manage.povo.ActivityVO;
import com.lkhd.manage.povo.CommentVo;
import com.lkhd.manage.povo.VolunteerVo;

import tk.mybatis.mapper.entity.Example;

/**
 * 活动服务实现
 * 
 * @author Administrator 2018-9-15 10:04:46
 */
@Service
@Transactional
public class ActivityServiceImpl implements ActivityService {

	@Autowired
	private ActivityMapper activityMapper;
	@Autowired
	private TickettypeMapper tickettypeMapper;
	@Autowired
	private ActivityuserplanMapper activityuserplanMapper;
	@Autowired
	private ActivitymultipartcategoryMapper activitymultipartcategoryMapper;
	@Autowired
	private TicketMapper ticketMapper;
	@Autowired
	private UsercommentMapper usercommentMapper;
	@Autowired
	private CategoryMapper categoryMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private AreasMapper areasMapper; 
	@Autowired
	private VolunteerMapper volunteerMapper; 

	/**
	 * 发布活动
	 * 
	 * @param activity           活动基本信息
	 * @param ticketTypes        票种
	 * @param acitivityUserPlans 活动人员安排
	 * @param categorys          多分类
	 * @return
	 */
	public int releaseActivity(Activity activity, List<Tickettype> ticketTypes,
			List<Activityuserplan> acitivityUserPlans, List<Activitymultipartcategory> categorys) {
		activity.setActivityid(IDUtil.getId());
		activity.setBrowsenum(0);
		activity.setCollectionnum(0);
		activity.setActivitynum(0);
		activity.setStatus(CategoryEnum.UP.getCode());// 上架
		// 插入活动
		int insertActivity = activityMapper.insert(activity);
		if (ticketTypes != null) {
			// 插入票种
			for (Tickettype tickettype : ticketTypes) {
				tickettype.setTickettypeid(IDUtil.getId());
				tickettype.setActivityid(activity.getActivityid());
				tickettypeMapper.insert(tickettype);
			}
		}
		if (acitivityUserPlans != null) {
			// 插入人员安排
			for (Activityuserplan activityuserplan : acitivityUserPlans) {
				activityuserplan.setAcitivityplanid(IDUtil.getId());
				activityuserplan.setActivityid(activity.getActivityid());
				activityuserplanMapper.insert(activityuserplan);
			}
		}
		if (categorys != null) {
			// 插入分类
			for (Activitymultipartcategory amc : categorys) {
				amc.setActivitymultipartcategoryid(IDUtil.getId());
				amc.setActivityid(activity.getActivityid());
				activitymultipartcategoryMapper.insert(amc);
			}
		}

		return insertActivity;
	}

	/**
	 * 下架活动(如果有用户报名不能下架)
	 * 
	 * @param ActivityId
	 * @param is         true 下架 false上架
	 * @return
	 */
	public int freezeActivity(Long ActivityId, boolean is) {
		int status = is==true?CategoryEnum.DOWN.getCode():CategoryEnum.UP.getCode();
		
		// 查询是否有人报名
		Ticket ticket = new Ticket();
		ticket.setActivityid(ActivityId);
		int selectCount = ticketMapper.selectCount(ticket);
		if(selectCount>0) {
			// 有人报名，不能下架
			throw new ActivityException(ActivityEnum.DELFAIL);
		}
		Activity activity = new Activity();
		activity.setActivityid(ActivityId);
		activity.setStatus(status);
		return activityMapper.updateByPrimaryKeySelective(activity);
	}

	/**
	 * 修改活动基本信息（人员安排，票种，分类，在其他方法中）
	 * 
	 * @param activity
	 * @return
	 */
	public int update(Activity activity) {
		return activityMapper.updateByPrimaryKeySelective(activity);
	}

	/**
	 * 添加活动票种
	 * 
	 * @param ticketType
	 * @return
	 */
	public int addTicketType(Tickettype ticketType) {
		ticketType.setTickettypeid(IDUtil.getId());
		return tickettypeMapper.insert(ticketType);
	}

	/**
	 * 删除活动票种(有人购买该票种不能删除)
	 * 
	 * @param ticketTypeId
	 * @return
	 */
	public int delTicketType(Long ticketTypeId) {
		// 查询是否有该票种被人报名
		Ticket ticket = new Ticket();
		ticket.setTickettypeid(ticketTypeId);
		int selectCount = ticketMapper.selectCount(ticket);
		if(selectCount>0) {
			// 有人报名，不能删除
			throw new ActivityException(ActivityEnum.DELTTPYEFAIL);
		}
		return tickettypeMapper.deleteByPrimaryKey(ticketTypeId);
	}

	/**
	 * 修改活动票种
	 * 
	 * @param ticketType
	 * @return
	 */
	public int updateTicketType(Tickettype ticketType) {
		return tickettypeMapper.updateByPrimaryKeySelective(ticketType);
	}

	/**
	 * 添加活动人员安排
	 * 
	 * @param acitivityUserPlan
	 * @return
	 */
	public int addActivityUserPlan(Activityuserplan acitivityUserPlan) {
		acitivityUserPlan.setAcitivityplanid(IDUtil.getId());
		return activityuserplanMapper.insert(acitivityUserPlan);
	}

	/**
	 * 删除活动人员安排
	 * 
	 * @param acitivityUserPlanId
	 * @return
	 */
	public int delActivityUserPlan(Activityuserplan acitivityUserPlan) {
		// 如果该活动只有一个负责人就不允许删除
		Activityuserplan query = new Activityuserplan();
		query.setActivityid(acitivityUserPlan.getActivityid());
		int selectCount = activityuserplanMapper.selectCount(query);
		if(selectCount==1) {
			// 必须要有主负责人
			throw new ActivityException(ActivityEnum.DELVOLUNTEER);
		}
		return activityuserplanMapper.deleteByPrimaryKey(acitivityUserPlan.getAcitivityplanid());
	}

	/**
	 * 修改活动人员安排
	 * 
	 * @param acitivityuserplan
	 * @return
	 */
	public int updateActivityUserPlan(Activityuserplan acitivityuserplan) {
		return activityuserplanMapper.updateByPrimaryKey(acitivityuserplan);
	}

	/**
	 * 活动添加分类
	 * 
	 * @param activityMultipartCategory
	 * @return
	 */
	public int addCategory(Activitymultipartcategory activityMultipartCategory) {
		activityMultipartCategory.setActivitymultipartcategoryid(IDUtil.getId());
		return activitymultipartcategoryMapper.insert(activityMultipartCategory);
	}

	/**
	 * 活动删除分类
	 * 
	 * @param activityMultipartCategory
	 * @return
	 */
	public int delCategory(Activitymultipartcategory activityMultipartCategory) {
		return activitymultipartcategoryMapper.deleteByExample(activityMultipartCategory);
	}

	/**
	 * 根据活动id获取活动评论
	 * 
	 * @param ActivityId
	 * @return
	 */
	public PageInfo<CommentVo> commentByActivityId(Long ActivityId, int pageNum, int pageSize) {
		Example example = new Example(Usercomment.class);
		example.createCriteria().andEqualTo("activityid", ActivityId);
		
		example.orderBy("createdate").desc();
		
		PageHelper.startPage(pageNum, pageSize);
		List<Usercomment> comms = usercommentMapper.selectByExample(example);
//		//  要包含用户的头像和昵称
		PageInfo<Usercomment> comments1 = new PageInfo<>(comms);
		PageInfo<CommentVo> comments2 = new PageInfo<>();
		BeanUtils.copyProperties(comments1, comments2);
		
		List<CommentVo> comments = new ArrayList<>();
		for(Usercomment commetn : comments1.getList()) {
			CommentVo temp = new CommentVo();
			BeanUtils.copyProperties(commetn, temp);
			User user = userMapper.selectByPrimaryKey(commetn.getUserid());
			temp.setUsername(user.getNickname());
			temp.setImgAddr(user.getImgaddr());
			comments.add(temp);
		}
		comments2.setList(comments);
		return comments2;
	}

	/**
	 * 根据活id获取活动票种
	 * 
	 * @param ActivityId
	 * @return
	 */
	public List<Tickettype> ticketTypeByActivityId(Long ActivityId) {
		Tickettype tickettype = new Tickettype();
		tickettype.setActivityid(ActivityId);
		
		return tickettypeMapper.select(tickettype);
	}

	/**
	 * 根据活动id获取活动分类
	 * 
	 * @param ActivityId
	 * @return
	 */
	public List<Category> categoryByActivityId(Long ActivityId) {
		Activitymultipartcategory amc = new Activitymultipartcategory();
		amc.setActivityid(ActivityId);
		List<Activitymultipartcategory> amcs = activitymultipartcategoryMapper.select(amc);
		
		List<Category> categorys = new ArrayList<>();
		for(Activitymultipartcategory tempAmc : amcs) {
			Category category = categoryMapper.selectByPrimaryKey(tempAmc.getCategoryid());
			categorys.add(category);
		}
		return categorys;
	}
	
	/**
	 * 	根据活id获取活动志愿者
	 * @param ActivityId
	 * @return
	 */
	public List<VolunteerVo> volunteerByActivityId(Long ActivityId) {
		Example example = new Example(Activityuserplan.class);
		example.createCriteria().andEqualTo("activityid", ActivityId);
		// 活动人员记录
		List<Activityuserplan>  activityuserplans= activityuserplanMapper.selectByExample(example);
		
		List<VolunteerVo> vols = new ArrayList<>();
		for(Activityuserplan aup : activityuserplans) {
			// 获取志愿者信息，对应的用户信息
			VolunteerVo vvo = new VolunteerVo();
			Volunteer volunteer = volunteerMapper.selectByPrimaryKey(aup.getVolunteerid());
			BeanUtils.copyProperties(volunteer, vvo);
			// 志愿者对应的用户信息
			User user = userMapper.selectByPrimaryKey(volunteer.getVolunteerid());//志愿者id就是用户id
			user.setPassword(null);
			vvo.setUser(user);
			vols.add(vvo);
		}
		
		return vols;
	}

	/**
	 * 根据活动id获取活动详细
	 * 
	 * @param ActivityId
	 * @return activityVo 包含：活动基本信息  活动分类  主负责人 地点
	 */
	public ActivityVO selectDetailById(Long ActivityId) {
		ActivityVO activityVO = new ActivityVO();
		Activity activity = activityMapper.selectByPrimaryKey(ActivityId);
		// 封装基本信息
		BeanUtils.copyProperties(activity, activityVO);
		// 地点
		Areas province = areasMapper.selectByPrimaryKey(activity.getProvinceid());
		Areas city = areasMapper.selectByPrimaryKey(activity.getCityid());
		Areas area = areasMapper.selectByPrimaryKey(activity.getAreaid());
		activityVO.setProvince(province);
		activityVO.setCity(city);
		activityVO.setArea(area);
		//活动分类
		List<Category> categorys = this.categoryByActivityId(ActivityId);
		activityVO.setCategorys(categorys);
		//主负责人
		List<VolunteerVo> volunteerVos = this.volunteerByActivityId(ActivityId);
		activityVO.setVolunteerVos(volunteerVos);
		
		return activityVO;
	}
	
	/**
	 * 根据活动id获取活动信息 【只包含地址】
	 * @param ActivityId
	 * @return
	 */
	public ActivityVO selectBaseById(Long ActivityId) {
		ActivityVO activityVO = new ActivityVO();
		Activity activity = activityMapper.selectByPrimaryKey(ActivityId);
		// 封装基本信息
		BeanUtils.copyProperties(activity, activityVO);
		// 封装其他信息
		Areas province = areasMapper.selectByPrimaryKey(activity.getProvinceid());
		Areas city = areasMapper.selectByPrimaryKey(activity.getCityid());
		Areas area = areasMapper.selectByPrimaryKey(activity.getAreaid());
		activityVO.setProvince(province);
		activityVO.setCity(city);
		activityVO.setArea(area);
		
		return activityVO;
	}
	
	/**
	 * 按条件查询活动列表
	 * 
	 * @return
	 */
	public PageInfo<Activity> selectAll(Example example, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<Activity> selectResult = activityMapper.selectByExample(example);
		PageInfo<Activity> page = new PageInfo<>(selectResult);
		return page;
	}
}
